package com.yexiong.client.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.common.client.view.ClientMainFrame;

import utils.DateChooser;

import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.Component;
import javax.swing.Box;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;

public class BillQueryDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField documNumField;
	private JTextField textField_1;
	private JTextField textField_2;
	private LoginFrame loginFrame;
	private BillPanel billPanel;
	private JCheckBox adminCheck;
	private JComboBox amdinBox;
	private JCheckBox documDateCheck;
	private JComboBox typeBox;
	private JCheckBox documTypeCheck;
	private JCheckBox documNumCheck;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
//		try {
//			BillQueryDialog dialog = new BillQueryDialog();
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	/**
	 * Create the dialog.
	 */
	public BillQueryDialog(LoginFrame loginFrame,BillPanel billPanel) {
		this.billPanel=billPanel;
		this.loginFrame=loginFrame;
		setTitle("\u5355\u636E\u67E5\u8BE2");
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			documNumCheck = new JCheckBox("\u5355\u636E\u53F7");
			documNumCheck.setBounds(31, 31, 82, 27);
			contentPanel.add(documNumCheck);
		}
		
		documTypeCheck = new JCheckBox("\u5355\u636E\u7C7B\u578B");
		documTypeCheck.setBounds(31, 71, 89, 27);
		contentPanel.add(documTypeCheck);
		
		documNumField = new JTextField();
		documNumField.setBounds(150, 32, 182, 24);
		contentPanel.add(documNumField);
		documNumField.setColumns(10);
		
		typeBox = new JComboBox();
		typeBox.setEditable(true);
		typeBox.setBounds(150, 72, 182, 24);
		contentPanel.add(typeBox);
		
		documDateCheck = new JCheckBox("\u5F00\u5355\u65F6\u95F4");
		documDateCheck.setBounds(31, 114, 89, 27);
		contentPanel.add(documDateCheck);
		
		textField_1 = new JTextField();
		textField_1.setBounds(150, 115, 86, 24);
		DateChooser dateChooser1 = DateChooser.getInstance("yyyy-MM-dd");
		dateChooser1.register(textField_1);
		contentPanel.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(246, 115, 86, 24);
		DateChooser dateChooser2 = DateChooser.getInstance("yyyy-MM-dd");
		dateChooser2.register(textField_2);
		contentPanel.add(textField_2);
		
		adminCheck = new JCheckBox("\u7ECF\u529E\u4EBA");
		adminCheck.setBounds(31, 155, 89, 27);
		contentPanel.add(adminCheck);
		
		amdinBox = new JComboBox();
		amdinBox.setEditable(true);
		amdinBox.setBounds(150, 156, 182, 24);
		contentPanel.add(amdinBox);
		
		Component horizontalStrut = Box.createHorizontalStrut(20);
		horizontalStrut.setBounds(237, 128, 12, 0);
		contentPanel.add(horizontalStrut);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("\u786E\u8BA4");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						new Thread(()->{
							addCondition();
						}).start();
						
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("\u53D6\u6D88");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	public void addCondition() {
		List<String> list=new ArrayList<>();
		if (documNumCheck.isSelected()) {
			list.add(documNumField.getText());
		}else {
			list.add("");
		}
		if (documTypeCheck.isSelected()) {
			String type=(String)typeBox.getSelectedItem();
			int index=type.indexOf("-");
			list.add(type.substring(0, index));
		}else {
			list.add("");
		}
		if (documDateCheck.isSelected()) {
			billPanel.textField.setText(textField_1.getText());
			billPanel.textField_1.setText(textField_2.getText());
		}
		if (adminCheck.isSelected()) {
			String type=(String)amdinBox.getSelectedItem();
			int index=type.indexOf("-");
			list.add(type.substring(0, index));
		}else {
			list.add("");
		}
		billPanel.list=list;
		billPanel.showDate(1, list);
	}
}
